﻿using System;
using static IchiaTester.MainForm;

namespace IchiaTester.Utils
{
    /// <summary>
    /// 日志内容
    /// </summary>
    public class LogContent
    {
        public string timeStamp; // 时间戳
        public string msgType; // 信息类型
        public string logDetail; // 日志详情
        public string errorMsg; // 错误信息
        public SystemState systemStatus; // 当前系统状态

        //定義事件
        public event Action<string> LogUpdated;
        private readonly object lockObject; // 锁对象


        public LogContent()
        {
            lockObject = new object();
            timeStamp = "";
            msgType = "";
            logDetail = "";
            errorMsg = "";
            systemStatus = SystemState.Idle;
        }

        public string TimeStamp
        {
            get
            {
                lock (lockObject)
                {
                    return timeStamp;
                }
            }
            set
            {
                lock (lockObject)
                {
                    timeStamp = value;
                }
            }
        }

        public string MsgType
        {
            get
            {
                lock (lockObject)
                {
                    return msgType;
                }
            }
            set
            {
                lock (lockObject)
                {
                    msgType = value;
                }
            }
        }

        public SystemState SystemStatus
        {
            get
            {
                lock (lockObject)
                {
                    return systemStatus;
                }
            }
            set
            {
                lock (lockObject)
                {
                    if (systemStatus != value)
                    {
                        systemStatus = value;
                        TimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        LogUpdated?.Invoke(CreateLogContent());
                    }
                }
            }
        }

        public string LogDetail
        {
            get
            {
                lock (lockObject)
                {
                    return logDetail;
                }
            }
            set
            {
                lock (lockObject)
                {
                    logDetail = value;
                }
            }
        }

        public string ErrorMsg
        {
            get
            {
                lock (lockObject)
                {
                    return errorMsg;
                }
            }
            set
            {
                lock (lockObject)
                {
                    errorMsg = value;
                }
            }
        }

        private string CreateLogContent()
        {
            lock (lockObject)
            {
                if (MsgType == "")
                {
                    MsgType = "Info";
                }

                string logTxt = "";
                logTxt += TimeStamp + ": ";
                logTxt += "| " + MsgType + " | ";
                logTxt += "SystemSatatus: " + "{" + SystemStatus + "} " + Environment.NewLine;
                if (!string.IsNullOrEmpty(LogDetail))
                {
                    logTxt += "Detail: " + LogDetail;
                    LogDetail = "";
                }

                if (!string.IsNullOrEmpty(ErrorMsg))
                {
                    logTxt += "Error: " + ErrorMsg;
                    ErrorMsg = "";
                }

                MsgType = "";
                logTxt += Environment.NewLine;
                return logTxt;
            }
        }
    }
}